//
//  BBCollectionNodeView.m
//  xNose
//
//  Created by ben smith on 12/09/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBCollectionNodeView.h"
#import "BBNode.h"
#import "BBDefaultController.h"
#import "BBNodeController.h"
#import "BBImageButtonFactory.h"

@implementation BBCollectionNodeView

- (id)initWithFrame:(NSRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		//[self setWantsLayer:YES];

		// add the disclosure button
		float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
		float triSize = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTriangleButtonSize"];
		NSRect buttonRect = NSMakeRect(NSMinX(self.bounds) + borderHeight, NSMidY(self.bounds) - (triSize/2.0), triSize, triSize);
		//TODO: make the disclosure a CALayer and animate it's rotation
		disclosure = [[BBImageButtonFactory disclosureButtonInRect:buttonRect] retain];
		[disclosure setTarget:self];
		[disclosure setAction:@selector(toggleDisclosure)];
		[self addSubview:disclosure];
		
		float buttonSize = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBigChartButtonSize"];
		NSRect contentRect = [backgroundRect bounds];
		
		float yOffset = NSMidY(contentRect) - (buttonSize/2.0) + 1;
		
		buttonRect = NSMakeRect(NSMaxX(contentRect) - buttonSize - borderHeight + 5, yOffset, buttonSize, buttonSize);
		
		NSButton * chartButton = [BBImageButtonFactory bigChartButtonInRect:buttonRect];
		[chartButton setTarget:self];
		[chartButton setAction:@selector(chartClick)];
		[self addSubview:chartButton];
	}
	return self;
}

-(void)chartClick
{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBOpenControllerNotification" object:self.node];
}


-(void)toggleDisclosure
{
	if (self.node.isOpen) {
		// then we need to expand the node
		[[BBNodeController sharedNodeController] collapseNode:self.node];
		return;
	}		
	[[BBNodeController sharedNodeController] expandNode:self.node];
}

-(void)drawNode
{
	[self.backgroundGradient drawInBezierPath:backgroundRect angle:90.0];		
}

-(void)setDisclosureTransparency
{
	if ([self.node hasChildren]) {
		[disclosure setEnabled:YES];
		[disclosure setTransparent:NO];
	} else {
		[disclosure setEnabled:NO];
		[disclosure setTransparent:YES];		
	}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	if (node != nil) [titleField setStringValue:self.node.title];	
	if (node != nil) [subtitleField setStringValue:node.subtitle];
	[self setDisclosureTransparency];
}

-(void)setNode:(BBNode*)aNode
{
	[node removeObserver:self forKeyPath:@"subtitle"];
	[node removeObserver:self forKeyPath:@"hasChildren"];
	[super setNode:aNode];
	if (node == nil) return;
	if (node.isOpen) [disclosure setState:NSOnState];
	[subtitleField setStringValue:node.subtitle];

	[aNode addObserver:self forKeyPath:@"hasChildren" options:NSKeyValueObservingOptionNew context:nil];
	[aNode addObserver:self forKeyPath:@"subtitle" options:NSKeyValueObservingOptionNew context:nil];
	[self setDisclosureTransparency];
}

- (void)setupTextAttributes
{
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float fieldHeight  = [BBDefaultController fontHeight:[self font]];
	float textInset = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTextInsetWidth"];
	
	NSRect titleFrame = NSMakeRect(NSMinX(self.bounds) + (textInset * 2.0), NSMaxY(self.bounds) - fieldHeight - (borderHeight * 2.5), NSWidth(self.bounds) - (textInset * 4.0), fieldHeight);
		
	
	// we actually have 2 fields
	titleField = [[NSTextField alloc] initWithFrame:titleFrame];
	[titleField setAlignment:NSCenterTextAlignment];
	[titleField setFont:[self font]];
	[titleField setTextColor:[self fontColor]];
	[titleField setDrawsBackground:NO];
	[titleField setBordered:NO];
	[titleField setSelectable:NO];
	[titleField setEditable:NO];

	[titleField setTarget:self];
	[titleField setAction:@selector(fieldDone:)];
	[titleField setDelegate:self];
	[self addSubview:titleField];
	
	NSFont * subFont = [NSFont fontWithName:@"MyriadPro-LightIt" size:12];
	
	fieldHeight  = [BBDefaultController fontHeight:subFont];
	NSRect subFrame = NSMakeRect(NSMinX(self.bounds) + (textInset * 2.0), NSMinY(self.bounds) + (borderHeight * 2.0), NSWidth(self.bounds) - (textInset * 4.0), fieldHeight);
	
	
	subtitleField = [[NSTextField alloc] initWithFrame:subFrame];
	[subtitleField setAlignment:NSCenterTextAlignment];
	[subtitleField setFont:subFont];
	[subtitleField setTextColor:[self fontColor]];
	[subtitleField setDrawsBackground:NO];
	[subtitleField setSelectable:NO];
	[subtitleField setEditable:NO];
	[subtitleField setBordered:NO];
	[subtitleField setTarget:self];
	[subtitleField setAction:@selector(subfieldDone:)];
	[subtitleField setDelegate:self];
	[self addSubview:subtitleField];
}	

- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
	self.node.title = [titleField stringValue];
	self.node.subtitle = [subtitleField stringValue];
}

- (void)subfieldDone:(id)sender
{
	self.node.subtitle = [subtitleField stringValue];
	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBReleaseEditNotification" object:nil];	
}

-(NSFont*)font 
{
	return [BBDefaultController regularMediumFont];
}

-(NSColor*)fontColor 
{
	return [NSColor whiteColor];
}

-(void)setupBackgroundRect
{
	// draws a nice container rounded rectangle lookin thing
	float collectionHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBCollectionHeight"];
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float disclosureOffset = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBDisclosureOffset"];
	collectionHeight -= borderHeight * 2.0;
	
	NSRect contentRect = NSMakeRect(NSMinX(self.bounds) + disclosureOffset + borderHeight, NSMidY(self.bounds) - (collectionHeight/2.0), NSWidth(self.bounds) - (disclosureOffset + borderHeight + borderHeight), collectionHeight);
	
	NSRect containRect = contentRect;
	containRect.origin.x = floor(containRect.origin.x) + 0.5;
	containRect.origin.y = floor(containRect.origin.y) + 0.5;
	containRect.size.width = floor(containRect.size.width);
	containRect.size.height = floor(containRect.size.height);
	
	self.backgroundRect = [NSBezierPath bezierPathWithRoundedRect:containRect xRadius:10 yRadius:10];
}

- (void)setupBackgroundGradient
{
	// create a basic gradient for the background of the view
	CGFloat red1   =   100.0 / 255.0;
	CGFloat green1 =   100.0 / 255.0;
	CGFloat blue1  =  100.0 / 255.0;
	
	CGFloat red2    =  80.0 / 255.0;
	CGFloat green2  =  80.0 / 255.0;
	CGFloat blue2   =  80.0 / 255.0;
	
	NSColor* gradientTop    = [NSColor colorWithCalibratedRed:red1 green:green1 blue:blue1 alpha:1.0];    
	NSColor* gradientBottom = [NSColor colorWithCalibratedRed:red2 green:green2 blue:blue2 alpha:1.0];
	
	NSGradient* gradient;
	gradient = [[NSGradient alloc] initWithStartingColor:gradientBottom endingColor:gradientTop];
	
	self.backgroundGradient = gradient;
	[gradient release];
}

- (void) dealloc
{
	[self setNode:nil];
	[super dealloc];
}

@end
